<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 缓存相关Controller v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace app\core\controller\site;


use app\core\model\CacheKey;
use app\core\service\FileService;

class CacheController extends SiteBaseController {

    /**
     * 构造函数
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function __construct() {
        parent::__construct();
    }

    /**
     * 清理缓存
     *
     * @return \think\response\View
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function clear() {
        if (is_submit()) {
            $types = input("post.type/a");
            if (empty($types)) {
                $this->error("请选择要更新的缓存类型");
            }
            if (isset($types['data']) && $types['data'] == 'on') {
                $ret = FileService::delDir(CACHE_PATH);
                CacheKey::clearExpiredKey();
                if (!$ret) {
                    $this->error("数据缓存目录" . str_replace(ROOT_PATH, "", CACHE_PATH) . "删除失败，请检查目录权限");
                }
            }
            if (isset($types['view']) && $types['view'] == 'on') {
                $ret = FileService::delDir(TEMP_PATH);
                if (!$ret) {
                    $this->error("页面缓存目录" . str_replace(ROOT_PATH, "", TEMP_PATH) . "删除失败，请检查目录权限");
                }
            }

            $this->success("缓存更新成功");
        }
        return view();
    }
}